import dataModel from "../model/index";

export const delDataController = async (ctx, next) => {
  let query = ctx.request.body;

  console.log(query);
  await dataModel.UserInfo.deleteMany(query);
  let result = await dataModel.UserInfo.find({});
  ctx.body = {
    status: 200,
    msg: "ok",
    length: Object.keys(result).length,
    data: result
  };
};

export const insertDataController = async ctx => {
  // post用ctx.request.body
  let query = ctx.request.body;

  // get使用ctx.query
  // let query = ctx.query;

  console.log("----------------------------------");
  console.log(ctx.request.body);
  console.log("----------------------------------");

  if (Object.keys(query).length > 0) {
    let rowObj = new dataModel.UserInfo(query);
    await rowObj.save();
    let result = await dataModel.UserInfo.find({});
    ctx.body = {
      status: 200,
      msg: "ok",
      length: Object.keys(result).length,
      data: result
    };
  }
};

export const getDataController = async (ctx, next) => {
  ctx.response.set({
    "Access-Control-Allow-Origin": "*"
  });
  ctx.response.type = "text/html";
  let result = await dataModel.UserInfo.find({});
  ctx.body = {
    status: 200,
    msg: "ok",
    length: Object.keys(result).length,
    data: result
  };
};
